package com.jcx.ldzj.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jcx.ldzj.dao.GenTableCretearMapper;
import com.jcx.ldzj.emun.MyCode;
import com.jcx.ldzj.emun.SystemCode;
import com.jcx.ldzj.entity.GenTableColumnInfo;
import com.jcx.ldzj.entity.GenTableCretearInfo;
import com.jcx.ldzj.entity.LoginUser;
import com.jcx.ldzj.service.IGenTableCretear;
import com.jcx.ldzj.utile.RedisUtil;
import com.jcx.ldzj.utile.SendEmail;
import com.jcx.ldzj.utile.StringUtile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@Transactional
public class GenTableCretearImp extends ServiceImpl<GenTableCretearMapper, GenTableCretearInfo> implements IGenTableCretear {

    @Autowired
    private RedisUtil redisclient;
    public List<GenTableCretearInfo> selectAll(String tableSchema){
        List<GenTableCretearInfo> genTableCretearInfos = this.baseMapper.selectTableAll(tableSchema);
        List<GenTableCretearInfo> list =new ArrayList<>();
        for (int i = 0; i < genTableCretearInfos.size(); i++) {
            GenTableCretearInfo genTableCretearInfo = genTableCretearInfos.get(i);
            List<GenTableColumnInfo> columnInfos = this.baseMapper.selectColumns(genTableCretearInfo.getTableName());
            genTableCretearInfo.setColumnList(columnInfos);
            list.add(genTableCretearInfo);
        }
        return list;
    }

    @Override
    public R getEmailCode(HttpServletRequest request, String userName, String myeMail) {
        if(StringUtile.isNull(userName)){
            return R.failed(MyCode.账号不存在_请重新输入);
        }
        String userMap = (String)redisclient.hGet(SystemCode.USER_MAP, userName);
        LoginUser loginUser = JSONObject.parseObject(userMap,LoginUser.class);
        if(StringUtile.isNull(userMap)){
            return R.failed(MyCode.账号不存在_请重新输入);
        }
        if(StringUtile.isNull(myeMail)){
            return R.failed(MyCode.输入的邮箱号为空_请核对后再试);
        }
        if(!myeMail.equals(loginUser.getMyeMail())){
            return R.failed(MyCode.输入的邮箱号与用户名不匹配_请核对后再试);
        }
        SendEmail sen = new SendEmail();
        sen.setReceiveMailAccount(myeMail);//填收件人的邮箱地址
        int emailCode = (int) ((Math.random() * 9 + 1) * 100000);//随机生成验证码
        HttpSession session = request.getSession();
        session.setAttribute("emailCode", emailCode);
        String strText = "您的验证码是 " + emailCode + ",用于模拟工作平台密码修改验证，请勿将验证码透露给他人，如非本人操作，请忽略";
        sen.setInfo(strText);
        sen.setRealName("昵称为："+loginUser.getRealName());
        try {
            sen.Send();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return R.ok("成功");
    }
}
